package com.yuyou.fn.crm.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.constant.ClassLevelEnum;
import com.yuyou.fn.common.constant.YesNoEnum;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.EnumUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.crm.constant.PriorityEnum;
import com.yuyou.fn.crm.constant.RegPossibleEnum;
import com.yuyou.fn.crm.constant.VisitWayEnum;
import com.yuyou.fn.crm.entity.VisitDetail;
import com.yuyou.fn.crm.mapper.VisitDetailMapper;
import com.yuyou.fn.crm.service.IVisitDetailService;
import com.yuyou.fn.crm.vo.VisitDetailQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * VisitDetail 表数据服务层接口实现类
 *
 */
@Service
public class VisitDetailServiceImpl extends SuperServiceImpl<VisitDetailMapper, VisitDetail,Long> implements IVisitDetailService {


    @Override
    public Page<VisitDetail> findVisitDetailByQuery(DataPermission dataPermission, VisitDetailQueryVo queryVo, int pageNo, int pageSize) {

        if(queryVo.getImportTimeOfEnd() != null ){
            queryVo.setImportTimeOfEnd(DateUtils.addDays(queryVo.getImportTimeOfEnd(),1 ));
        }

        if(queryVo.getVisitTimeOfEnd() != null ){
            queryVo.setVisitTimeOfEnd(DateUtils.addDays(queryVo.getVisitTimeOfEnd(),1 ));
        }

        if(queryVo.getAlloCreateTimeOfEnd() != null ){
            queryVo.setAlloCreateTimeOfEnd(DateUtils.addDays(queryVo.getAlloCreateTimeOfEnd(),1 ));
        }

        if(queryVo.getFollowCreateTimeOfEnd() != null ){
            queryVo.setFollowCreateTimeOfEnd(DateUtils.addDays(queryVo.getFollowCreateTimeOfEnd(),1 ));
        }


        List<Long> campusIds = dataPermission.getCampusIds();
        Example<VisitDetail> example = VisitDetail.createExample();
        example.orderBy("visitTime");
        Example.Criteria criteria = example.createCriteria();

        if(queryVo.getInClass() != null ){
            if(queryVo.getInClass() == 0 ){
                criteria.andEqualTo("regClassAmount",0 );
            }else{
                criteria.andGreaterThan("regClassAmount" , 0 );
            }
            queryVo.setInClass(null);
        }



        criteria.andIn("willRegCampusId" , campusIds );
        CriteriaUtils.fillCondition(criteria,queryVo );
        if (queryVo.getHaveReg()!=null){
            if (queryVo.getHaveReg()==1){
                criteria.andGreaterThanOrEqualTo("firstRegNum",1);
            }else if (queryVo.getHaveReg()==0){
                criteria.andCondition("(first_reg_num is null or first_reg_num=0 )");
            }
        }


        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public SXSSFWorkbook exportVisitDetail2Excel(DataPermission dataPermission, VisitDetailQueryVo queryVo) {
        Page<VisitDetail> page = this.findVisitDetailByQuery(dataPermission , queryVo,1 ,100000 ) ;

        SXSSFWorkbook workbook = new SXSSFWorkbook();
        SXSSFSheet sheet = workbook.createSheet("回访记录");

        String[] titles  = new String[]{"学生姓名" ,"学生学号","电话号码","年级","就读学校","是否在读","区域","活动名称","紧急程度","分配校区","导入时间","抢占人","回访人","回访时间","回访形式","报读意愿","意向科目","意向班型","回访内容","是否成交","回收时间"} ;
        SXSSFRow firstRow = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }

        int rowIndex  = 1 ;
        for(VisitDetail visitDetail : page.getRecords() ){
            SXSSFRow row = sheet.createRow(rowIndex++);

            int c = 0;
            SXSSFCell cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getStudentName());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getStudentNo());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getPhoneNo());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getGradeName());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getSchool());

            cell = row.createCell(c++);
            cell.setCellValue(YesNoEnum.values()[visitDetail.getRegClassAmount() == null || visitDetail.getRegClassAmount() == 0 ? 0 : 1].name());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getAreaTeamName());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getProjectName());


            cell = row.createCell(c++);
            cell.setCellValue(PriorityEnum.values()[visitDetail.getPriority()].name());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getWillRegCampusName());

            cell = row.createCell(c++);
            cell.setCellValue(Utils.formatDate(visitDetail.getImportTime(),"yyyy-MM-dd HH:mm"));

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getFollowOperatorName());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getVisitOperatorName());

            cell = row.createCell(c++);
            cell.setCellValue(Utils.formatDate(visitDetail.getVisitTime(),"yyyy-MM-dd HH:mm"));

            cell = row.createCell(c++);
            cell.setCellValue(VisitWayEnum.values()[visitDetail.getVisitWay()].name());

            cell = row.createCell(c++);
            cell.setCellValue(RegPossibleEnum.values()[visitDetail.getWillRegPossible()].name() );

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getWillRegSubjectName());


            cell = row.createCell(c++);
            cell.setCellValue( EnumUtils.getName(ClassLevelEnum.values() ,visitDetail.getWillRegRegLevel()) );

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getVisitContent());

            cell = row.createCell(c++);
            cell.setCellValue(visitDetail.getFirstRegNum()!=null && visitDetail.getFirstRegNum()>0?"是":"否");

            cell = row.createCell(c++);
            cell.setCellValue(Utils.formatDate(visitDetail.getRecycleTime(),"yyyy-MM-dd HH:mm"));
        }
        return workbook;
    }
}